<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('BusinessConfig.machineReview.state')">
          <el-select v-model="searchForm.status" :placeholder="$t('reviewRiskList.riskSetting.prompt.type')">
            <el-option
              v-for="item in $t('BusinessConfig.machineReview.status1')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('BusinessConfig.machineReview.stageName')">
          <el-input v-model="searchForm.flowName" clearable :placeholder="$t('BusinessConfig.machineReview.prompt.stageName')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-row>
        <el-button @click="add">
          {{ $t('BusinessConfig.machineReview.add') }}
        </el-button>
        <el-button @click="updateData">
          {{ $t('BusinessConfig.machineReview.update') }}
        </el-button>
      </el-row>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        class="Top"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          :label="tableText['id']"
          min-width="100"
        />
        <el-table-column
          prop="flowName"
          :label="tableText['stageName']"
          min-width="150"
        />
        <el-table-column
          prop="previousStep"
          :label="tableText['previous']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setName(scope.row['previousStep']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="nextStepPass"
          :label="tableText['through']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ setName(scope.row['nextStepPass']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="nextStepRefuse"
          :label="tableText['rejection']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ setName(scope.row['nextStepRefuse']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="employeeName"
          :label="tableText['updater']"
          min-width="150"
        />
        <el-table-column
          prop="modifyTime"
          :label="tableText['updateTime']"
          min-width="180"
        >
          <template slot-scope="scope">
            {{ scope.row['nextStepRefuse'] ? setTime(scope.row['modifyTime']) : '' }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          prop="status"
          :label="tableText['state']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ setState(scope.row['status']) }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="editInfo(scope.row)">{{ $t('common.operate.edit') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <risk-machine-review-dialog
      :form-type="0"
      :show-dialog="editDialog"
      :choose-item="chooseItem"
      :select-data-refuse="selectDataRefuseData"
      :select-data-by="selectDataByData"
      :select-data="selectAllData"
      :check-parameters-data="checkParametersData"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
    <review-check-dialog
      :title-text="titleText"
      :show-dialog="checkDialog"
      :check-item="checkItem"
      @change-dialog="changeCheckDialog"
      @edit-success="editCheckSuccess"
    />
  </el-row>
</template>

<script>
import { getRiskReview, getReview, systemCheck } from '@/api/risk'
import riskMachineReviewDialog from '@/components/risk/businessConfig/risk-machineReview-dialog'
import reviewCheckDialog from '@/components/risk/businessConfig/review-check-dialog'
import { formatDate } from '@/utils/method'
export default {
  name: 'ManualReview',
  components: {
    riskMachineReviewDialog,
    reviewCheckDialog
  },
  data() {
    return {
      loading: true,
      editDialog: false,
      checkDialog: false,
      tableText: {},
      tableData: [],
      titleText: null,
      checkParametersData: [],
      allSelectData: [],
      selectDataBy: [],
      selectDataRefuse: [],
      selectDataByData: [],
      selectDataRefuseData: [],
      selectData: [],
      selectAllData: [],
      searchForm: this.params(),
      chooseItem: {
        id: null,
        flowName: null,
        checkParameters: null,
        nextStepPass: null,
        nextStepRefuse: null,
        previousStep: null,
        status: null,
        type: null
      },
      checkItem: {
        resultText: null,
        resultMsg: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('BusinessConfig.machineReview')
    this.getCheckParameters()
    this.getSelectData()
    setTimeout(() => {
      this.getData()
    }, 100)
  },
  methods: {
    params() {
      return {
        type: 1,
        flowName: null,
        status: ''
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    updateData() {
      this.titleText = this.$t('BusinessConfig.machineReview.checkManualReview')
      systemCheck()
        .then(res => {
          if (res.success) {
            const obj = {
              resultText: 0,
              resultMsg: null
            }
            this.checkSet(obj)
            this.checkDialog = true
          } else {
            const obj = {
              resultText: 1,
              resultMsg: res.message
            }
            this.checkSet(obj)
            this.checkDialog = true
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    getData() {
      this.loading = true
      getReview(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    setTime(time) {
      return formatDate(new Date(time), 'yyyy-MM-dd hh:mm:ss')
    },
    setName(data) {
      const name = this.allSelectData.filter(item => {
        return item['id'] === data
      })
      return (name.length > 0 && name[0]['flowName']) ? name[0]['flowName'] : ''
    },
    setState(data) {
      const name = this.$t('BusinessConfig.machineReview.status').filter(item => {
        return item.value === data
      })
      return (name.length > 0 && name[0]['desc']) ? name[0]['desc'] : ''
    },
    getCheckParameters() {
      const data = {
        isPaging: false
      }
      getRiskReview(data)
        .then(res => {
          if (res.success) {
            this.checkParametersData = (res.data).filter(v => {
              return v['status'] === 'ON_LINE'
            })
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getSelectData() {
      getReview({})
        .then(res => {
          this.loading = false
          if (res.success) {
            if (res.data && res.data['length'] === 0) {
              this.selectDataBy = this.$t('BusinessConfig.machineReview.stageDataByManualReview')
              this.selectDataRefuse = this.$t('BusinessConfig.machineReview.stageDataRefuseManualReview')
              this.selectData = this.$t('BusinessConfig.machineReview.stageData')
              this.allSelectData = this.$t('BusinessConfig.machineReview.stageData')
            } else {
              this.allSelectData = res.data
              this.selectData = res.data.filter(v => {
                return v['status'] === 1
              })
              this.selectDataBy = this.selectData.concat(this.$t('BusinessConfig.machineReview.stageDataByManualReview'))
              this.selectDataRefuse = this.selectData.concat(this.$t('BusinessConfig.machineReview.stageDataRefuseManualReview'))
              this.selectData = this.selectData.concat(this.$t('BusinessConfig.machineReview.stageData'))
              this.allSelectData = this.allSelectData.concat(this.$t('BusinessConfig.machineReview.stageData'))
              this.selectDataByData = this.selectDataBy
              this.selectDataRefuseData = this.selectDataRefuse
              this.selectAllData = this.selectData
            }
            this.tableData = res.data
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    add() {
      this.choose({})
      this.editDialog = true
    },
    editInfo(data) {
      this.choose(data)
      this.selectDataByData = this.selectDataBy.filter(v => {
        return v['id'] !== data.id
      })
      this.selectDataRefuseData = this.selectDataRefuse.filter(v => {
        return v['id'] !== data.id
      })
      this.selectAllData = this.selectData.filter(v => {
        return v['id'] !== data.id
      })
      this.editDialog = true
    },
    changeDialog(param) {
      this.editDialog = param
      setTimeout(() => {
        this.choose({})
      }, 10)
    },
    changeCheckDialog(param) {
      this.checkDialog = param
      setTimeout(() => {
        this.checkSet({})
      }, 10)
    },
    editCheckSuccess() {
      this.getData()
    },
    editSuccess() {
      this.getData()
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.flowName = param.flowName
      this.chooseItem.checkParameters = param.checkParameters ? param.checkParameters.split('_') : []
      this.chooseItem.nextStepPass = param.nextStepPass
      this.chooseItem.nextStepRefuse = param.nextStepRefuse
      this.chooseItem.previousStep = param.previousStep
      this.chooseItem.status = param.status
      this.chooseItem.type = param.type
    },
    checkSet(param) {
      this.checkItem.resultText = param.resultText
      this.checkItem.resultMsg = param.resultMsg
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
  .Top{
    margin-top: 10px;
  }
</style>
